package net.wenku8.reader.ui.lite

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.dp
import net.wenku8.reader.ui.theme.LiteGray
import kotlin.math.absoluteValue


@Composable
fun Modifier.smallPadding(): Modifier {
    return this then Modifier
        .padding(1.dp)
}

@Composable
fun Modifier.mediumPadding(): Modifier {
    return this then Modifier
        .padding(5.dp)
}

@Composable
fun Modifier.largePadding(): Modifier {
    return this then Modifier
        .padding(16.dp)
}


@Composable
fun Modifier.iconSize(): Modifier {
    return this then Modifier
        .size(28.dp)
}

@Composable
fun Modifier.iconMinSize(): Modifier {
    return this then Modifier
        .size(24.dp)
}


@Composable
fun Modifier.liteInline(): Modifier {
    return this then Modifier
        .fillMaxWidth()
        .padding(top = 6.dp, bottom = 6.dp, start = 20.dp, end = 20.dp)
}


@Composable
fun Modifier.liteOutline(): Modifier {
    return this then Modifier
        .padding(bottom = 1.dp)
        .background(LiteGray)
}

@Composable
fun Modifier.cardImage(): Modifier {
    return this then Modifier
        .size(70.dp, 90.dp)
}



@Composable
fun Modifier.onSwipe(onSwiped : (left : Boolean) -> Unit = {}): Modifier {
    var dragDistance by remember { mutableFloatStateOf(0f) }
    return this then Modifier
        .pointerInput(Unit) {
            detectHorizontalDragGestures(
                onDragEnd = {
                    if (dragDistance.absoluteValue >= 16) {
                        if (dragDistance > 0) {
                            onSwiped(false)
                        } else {
                            onSwiped(true)
                        }
                    }
                    // 重置距离
                    dragDistance = 0f
                }
            ) { change, dragAmount ->
                dragDistance += dragAmount
                change.consume() // 消耗事件，防止进一步传播
            }
        }
}


@Composable
fun Modifier.onPullDown(onPullDown : () -> Unit): Modifier {
    var pullDown by remember { mutableStateOf(false) }
    return this then Modifier
        .pointerInput(Unit) {
            detectHorizontalDragGestures(
                onDragEnd = {
                    if (pullDown){
                        onPullDown()
                    }
                }
            ){ change, dragAmount ->
                if (dragAmount > 20) { // 向下滑动
                    pullDown = true;
                }
                change.consume() // 消耗事件，防止进一步传播
            }
        }
}

@Composable
fun Modifier.onClick(onClick : () -> Unit): Modifier {
    return this then Modifier
        .clickable(
            indication = null,
            interactionSource = remember { MutableInteractionSource() }
        ) {
            onClick()
        }
}

@Composable
fun Modifier.onTap(onClick : (Offset) -> Unit = {}, onLongPress : () -> Unit = {}): Modifier {
    return this then Modifier
        .pointerInput(Unit) {
            detectTapGestures(
                onLongPress = {
                    // 长按时的处理逻辑
                    onLongPress()
                },
                onTap = {
                    onClick(it)
                }
            )
        }
}